﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using eCommMongo.Core;
using eCommMongo.Service.Posts;
using eCommMongo.Service.Tags;
using eCommMongo.Web.Models.Customers;
using eCommMongo.Web.Models.Tags;

namespace eCommMongo.Web.Controllers
{

    public class Tag
    {
        public string id { get; set; }
        public string label { get; set; }
        public string value { get; set; }
    }

    public class TagsController : Controller
    {


        private int pageSize = 2;

        private ITagService _tagService;
        private IPostService _postService;
        private IWorkContext _workContext;


        public TagsController(ITagService tagService, IPostService statusService, IWorkContext workContext)
        {
            _tagService = tagService;
            _postService = statusService;
            _workContext = workContext;
        }

        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        public ActionResult ChooseIneterstedTags(bool isFromRegist)
        {
            var tags = _tagService.FindTags(0, pageSize);
            var interestedTags = _tagService.FindInterestedTags(_workContext.CurrentCustomer.Id);
            var selectedTags = interestedTags == null ? null : interestedTags.Select(c => c.Id + "_" + c.TagName).ToList();

            var model = new CustomerTagModel()
            {
                TagsAvailable = tags,
                IsFromRegist = isFromRegist,
                SelectedTags = selectedTags
            };
            ViewBag.Model = model;
            return View();
        }

        //public ActionResult List(int? pageIndex)
        //{
        //    var tags = _tagService.FindTags(pageIndex.HasValue ? pageIndex.Value : 0, pageSize);
        //    var model = new TagListModel {TagLists = PostMapper.ToPagedTagList(tags)};
        //    return View(model);

        //}



        public ActionResult MyInterestedTags(int? pageIndex)
        {
            var tags = _tagService.FindInterestedTags(_workContext.CurrentCustomer.Id.ToString(), pageIndex.HasValue ? pageIndex.Value : 0, pageSize);
            var model = new TagListModel { TagLists = tags };
            return View(model);
        }

        public JsonResult GetTags(string term)
        {
            var tags = _tagService.FindTags(term);
            if (tags != null)
            {
                var result = tags.Select(c => new Tag() { id = c.Id.ToString(), label = c.TagName, value = c.TagValue }).ToList();
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }

        }
    }
}
